<template>
  <view class="box">
    <!-- 搜索 -->
    <view class="search-box" @click="onSearch">
      <search-bar></search-bar>
    </view>
    <!-- 轮播图 -->
    <view>
      <swiper class="swiper-list" indicator-dots autoplay circular>
        <swiper-item v-for="item in swipers" :key="item.goods_id">
          <image class="swiper-img" :src="item.image_src" mode="scaleToFill" />
        </swiper-item>
      </swiper>
    </view>
    <!-- 导航栏 -->
    <view class="nav-list">
      <image
        class="nav-img"
        v-for="item in catitems"
        :key="item.name"
        :src="item.image_src"
        mode="scaleToFill"
        @click="toCatitems(item.name)"
      />
    </view>
    <!-- 楼层数据 -->
    <view v-for="item in floordata" :key="item.floor_title.name">
      <!-- 标题 -->
      <image
        class="floor-title"
        :src="item.floor_title.image_src"
        mode="scaleToFill"
      />
      <!-- 内容 -->
      <view class="floor-img-box">
        <!-- 左边 -->
        <view>
          <image
            :style="{ width: item.product_list[0].image_width + 'rpx' }"
            :src="item.product_list[0].image_src"
            mode="widthFix"
          />
        </view>
        <!-- 右边 -->
        <view class="right-img-box">
          <image
            v-for="(subitem, index) in item.product_list"
            v-show="index > 0"
            :key="subitem.name"
            :src="subitem.image_src"
            :style="{ width: subitem.image_width + 'rpx' }"
            mode="widthFix"
          />
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data () {
    return {
      swipers: [],
      catitems: [],
      floordata: []
    }
  },
  onLoad () {
    this.getSwiperList()
    this.getCatitemsData()
    this.getFloorData()
  },

  methods: {
    //搜索
    onSearch () {
      // console.log(111)
      uni.navigateTo({ url: '/subpkg/search/index' })
    },
    // 轮播图
    async getSwiperList () {
      const res = await uni.$request({
        url: 'home/swiperdata'
      })
      // console.log(res)
      if (res.meta.status === 200) {
        this.swipers = res.message
      }
    },
    // 导航栏
    async getCatitemsData () {
      const res = await uni.$request({
        url: 'home/catitems'
      })
      // console.log(res)
      if (res.meta.status === 200) {
        this.catitems = res.message
      }
    },
    //分类列表
    async getFloorData () {
      const res = await uni.$request({
        url: 'home/floordata'
      })
      // console.log(res)
      if (res.meta.status === 200) {
        this.floordata = res.message
      }
    },
    //跳转到分类
    toCatitems (name) {
      if (name === '分类') {
        //跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
        uni.switchTab({ url: '/pages/category/index' })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
//解决搜索框固定定位塌陷问题
.box::before {
  content: '';
  display: flex;
  clear: both;
  /* height: 0;overflow: hidden; 为了解决 IE 浏览器的兼容问题 */
  height: 50px;
  overflow: hidden;
  /* visibility:hidden;为了去隐藏content中的内容 */
  visibility: hidden;
}
//搜索
.search-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 2;
}
//轮播图
.swiper-list {
  display: flex;
  height: 400rpx;
  .swiper-img {
    width: 100%;
    height: 100%;
  }
}
// 导航栏
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

//分类列表
.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
